#coding:utf8
lc_cryptkey = 62
def lc_encrypt(s):
	try:
		s=s.decode('gbk')
	except:
		pass
	try:
		s=s.encode('utf8')
	except:
		pass
	b = bytearray(s)
	n = len(b)
	c = bytearray(n*2)
	j = 0
	for i in range(0, n):
		b1 = b[i]
		b2 = b1 ^ lc_cryptkey
		c1 = b2 % 16
		c2 = b2 // 16
		c1 = c1 + 65
		c2 = c2 + 65
		c[j] = c1
		c[j+1] = c2
		j = j+2
	return c

def lc_decrypt(s):
	try:
		s=s.decode('gbk')
	except:
		pass
	try:
		s=s.encode('utf8')
	except:
		pass
	c = bytearray(s)
	n = len(c)
	if n % 2 != 0:
		raise TypeError("DecryptFailed")
	n = n // 2
	b = bytearray(n)
	j = 0
	for i in range(0, n):
		c1 = c[j]
		c2 = c[j+1]
		j = j+2
		c1 = c1 - 65
		c2 = c2 - 65
		b2 = c2*16 + c1
		b1 = b2^ lc_cryptkey
		b[i]= b1
	return b

#s="大家好"
#s="大家好".decode('utf8')
s="you 人are生 best".decode('utf8').encode('gbk')

s1 = lc_encrypt(s)
s2 = lc_decrypt(s1)
print s1,'\n',s2
